/**   
* @Title: IODpConfigController.java
* @Package cn.edu.sjtu.se.dslab.controller
* @Description: the description of the io device
* @author Thinker
* @date 2013-9-2 上午10:57:59
* @version V1.0
*/ 
package cn.edu.sjtu.se.dslab.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.sjtu.se.dslab.component.data.AvionicsEquipment;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.component.dp.parse.Property;
import cn.edu.sjtu.se.dslab.service.IAvionicsEquipmentService;
import cn.edu.sjtu.se.dslab.service.IDesignPatternComponentService;

/** 
 * @ClassName: IODpConfigController
 * @Description: config the io design pattern component
 * @author longshaohang
 * @date 2013-9-2 上午10:57:59
 * 
 */
@Controller
public class IODpConfigController {
	
	@Resource(name="avionicsEquipmentService")
	IAvionicsEquipmentService avionicsEquipmentService;
	
	@Resource(name="designPatternComponentService")
	IDesignPatternComponentService designPatternComponentService;
	
	@RequestMapping(value = "/ioDesignPattern/{type}/add", method = RequestMethod.POST)
	public String addDpcItem(@PathVariable(value="type")String type,
			@RequestParam String equipment, @RequestParam String bus_type,
			@RequestParam String equipment_number, @RequestParam String dpcUri, 
			@RequestParam long projectId){
		
		Map<String,String> propertyMap = new HashMap<String, String>();
		propertyMap.put("equipment", equipment);
		propertyMap.put("bus_type", bus_type);
		propertyMap.put("equipment_number", equipment_number);
		designPatternComponentService.addDpcItem(propertyMap, dpcUri);
		return toURL(type, projectId);
	}
	
	@RequestMapping(value = "/ioDesignPattern/info", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> getDpcItemInfo(@RequestBody Map<String, String> jsonStrings){
		DesignPatternInstanceBean dpiBean = designPatternComponentService.getDesignPatternInstanceBean(jsonStrings.get("dpcUri"));
		Map<String, Property> instance = dpiBean.getInstanceByName("equipment", jsonStrings.get("equipmentUri"));
		Map<String, String> mappingMap = new HashMap<String, String>();
		
		AvionicsEquipment innerEquipment = avionicsEquipmentService.getAvionicsEquipmentByUri(instance.get("equipment").getValue());
		mappingMap.put("inner_equipment_info", innerEquipment.getName());
		//bus type
		AvionicsEquipment busType = avionicsEquipmentService.getAvionicsEquipmentByUri(instance.get("bus_type").getValue());
		mappingMap.put("bus_type_info", busType.getName());
		mappingMap.put("equipment_number_info", instance.get("equipment_number").getValue());
		
		return mappingMap;
	}
	
	
	@RequestMapping(value = "/ioDesignPattern/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> deleteDpcItem(@RequestParam String name, @RequestParam String equipmentUri, 
			@RequestParam String dpcUri){
		designPatternComponentService.deleteDpcItem(name, equipmentUri, dpcUri);
		Map<String, String> resultMap = new HashMap<String,String>();
		resultMap.put("result", Boolean.TRUE.toString());
		return resultMap;
		//return "redirect:../" + dpcUri.replace(':', '/');
	}
	
	private String toURL(String type, long projectId){
		StringBuilder url = new StringBuilder("redirect:/");
		url.append(type).append("/").append(projectId).append("/design_pattern_component/IO/edit");
		return url.toString();
	}
}
